/**
 * Created by yu on 16/9/3.
 */

var http = require('http');
var url = require('url');
var fs = require('fs');
var queryString = require('querystring');

var server = http.createServer(function(req,res){
    var urlObj = url.parse(req.url,true);
    if(urlObj.pathname == '/'){
        res.setHeader("200",{'Content-type':'text/html;charset=utf-8'});
        fs.readFile('./index.html',function(err,data){
            res.write(data);
            res.end();
        })
    }else if(urlObj.pathname=='/post'){
        //获取客户端请求头的content-type值
        var result = '';
        req.on('data',function(data){
            result += data;
        })

        req.on('end',function(){
            var contentType = req.headers['content-type'];
            console.log(contentType);
            //如果是查询字符串,怎么解析
            if(contentType == 'application/x-www-form-urlencoded'){
                var data = queryString.parse(result);
                console.log(data);
            }else if(contentType == 'application/json'){//如果发来的是json怎么解析
                var data = JSON.parse(result);
                console.log(data);
            }
        })
    }
})

server.listen(8080,'localhost');

